package com.div.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/**
 * author: wangrui
 * date : 2018/3/5
 * description :popupwindow
 */

public class CustomPopwindow {

    private Context context;
    private PopupWindow popupWindow;
    //默认宽高
    private int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    private int hight = LinearLayout.LayoutParams.WRAP_CONTENT;
    private View view;
    private OnDismissListener mOnDismissListener;
    private static final int FULL_SCREEN_FLAG = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_FULLSCREEN;

    public CustomPopwindow(Context context) {
        this.context = context;
    }

    public CustomPopwindow setView(int resource) {
        view = LayoutInflater.from(context).inflate(resource, null);
        popupWindow = new PopupWindow(view, width, hight);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mOnDismissListener != null) {
                    mOnDismissListener.onDismiss();
                }
            }
        });

        return this;
    }

    public View getMenuView() {
        return view;
    }

    public CustomPopwindow setWidthAndHight(int width, int hight) {
        this.width = width;
        this.hight = hight;
        return this;
    }

    //是否获取焦点
    public CustomPopwindow setFocusable(boolean focusable) {
        popupWindow.setFocusable(focusable);
        return this;
    }

    //点击外部区域是否关闭popupwindow
    public CustomPopwindow setOutsideTouchable(boolean touchable) {
        popupWindow.setOutsideTouchable(touchable);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return this;
    }

    public void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();

        }
    }

    //设置背景透明度
    public CustomPopwindow setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = alpha;
//        ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ((Activity) context).getWindow().setAttributes(lp);
        return this;
    }

    //设置进出动画
    public CustomPopwindow setAnimationStyle(int animationStyle) {
        popupWindow.setAnimationStyle(animationStyle);
        return this;
    }

    public void showAtLocationFullScreen(View view) {
        Log.e("DDW", "showAtLocation");
        int[] location = new int[2];
        view.getLocationInWindow(location);
        //获取PopupWindow中View的宽高
        getMenuView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int measuredWidth = getMenuView().getMeasuredWidth();
        int measuredHeight = getMenuView().getMeasuredHeight();
        setFocusable(false);
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY,
                (location[0] + view.getWidth() / 2) - measuredWidth / 2,
                location[1] - measuredHeight);
        setFullScreen();
        setFocusable(true);
        update();
    }

    public void showAtLocation(View view) {
        Log.e("DDW", "showAtLocation");
        int[] location = new int[2];
        view.getLocationInWindow(location);
        //获取PopupWindow中View的宽高
        getMenuView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int measuredWidth = getMenuView().getMeasuredWidth();
        int measuredHeight = getMenuView().getMeasuredHeight();
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY,
                (location[0] + view.getWidth() / 2) - measuredWidth / 2,
                location[1] - measuredHeight);
    }

    public CustomPopwindow showAsDropDown(View view) {
        popupWindow.showAsDropDown(view);
        return this;
    }

    public CustomPopwindow showAsDropDown(View view, int xoff, int yoff) {
        popupWindow.showAsDropDown(view, xoff, yoff);
        return this;
    }

    public CustomPopwindow showAsDropDown(View view, int xoff, int yoff, int gravity) {
        popupWindow.showAsDropDown(view, xoff, yoff, gravity);
        return this;
    }

    public CustomPopwindow setInputMethodMode(int mode) {
        popupWindow.setInputMethodMode(mode);
        return this;
    }

    public void setFullScreen() {
        popupWindow.getContentView().setSystemUiVisibility(FULL_SCREEN_FLAG);
    }

    public void update() {
        popupWindow.update();
    }

    public boolean isShowing() {
        return popupWindow.isShowing();
    }

    public void setOnDismissListener(OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
    }

    public interface OnDismissListener {
        public void onDismiss();
    }
}
